package cn.wolfcode.luowowo.member.server;

import cn.wolfcode.luowowo.common.exception.LogistException;
import cn.wolfcode.luowowo.member.domain.IteamShop;
import cn.wolfcode.luowowo.member.domain.UserIteam;
import cn.wolfcode.luowowo.member.mapper.IteamShopMapper;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.mapper.UserIteamMapper;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;

import java.util.List;

@Service
public class UserIteamServiceImpl implements IUserIteamService{
    @Autowired
    private UserIteamMapper userIteamMapper;
    @Autowired
    private IteamShopMapper iteamShopMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;


    @Override
    public void buyiteam(Long uid, Long pid) throws LogistException{
        IteamShop iteamShop = iteamShopMapper.selectMyIteamsById(pid);
        //判断道具是否卖完
        if (iteamShop.getNum() <= 0) {
            throw new LogistException("道具卖完了!");
        }
        //判断钱是否充足
        Long gold = userInfoMapper.selectgold(uid);
        if (gold < iteamShop.getPrice()){
            throw new LogistException("余额不足!");
        }
        int n =userIteamMapper.selectByUid(uid,pid);
        //判断是否已经购买
        if (n==1){
            throw new LogistException("您已经购买过了!");
        }
        //金币减少
        userInfoMapper.subgold(uid,iteamShop.getPrice());
        //道具数量减少
        iteamShopMapper.subnum(pid);
        //建立关系
        userIteamMapper.insert(uid,pid);

    }
}
